//
//  ByCellRedConditionOrValid.m
//  mijifen_ios
//
//  Created by mc on 2017/10/25.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellRedConditionOrValid.h"

@interface ByCellRedConditionOrValid()<UITextFieldDelegate>
@end

@implementation ByCellRedConditionOrValid
{
    UIView      *_bgView;
    UILabel     *_titleLabel;
    UILabel     *_secTitleLabel;
    UITextField *_textField;
    UIView      *_lineView;
    UILabel     *_thrTitleLabel;
    NSString    *_title;
    NSString    *_secTitle;
    NSString    *_thrTitle;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView andTitle:(NSString *)title andSecTitle:(NSString *)secTitle andThrTitle:(NSString *)thrTitle{
    
    static NSString *cellIdentifier = @"ByCellRedConditionOrValid";
    ByCellRedConditionOrValid *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier andTitle:title andSecTitle:secTitle andThrTitle:thrTitle];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andTitle:(NSString *)title andSecTitle:(NSString *)secTitle andThrTitle:(NSString *)thrTitle{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _title = title;
        _secTitle = secTitle;
        _thrTitle = thrTitle;
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard) name:@"hideAimKeyBoard" object:nil];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _bgView = [[UIView alloc] init];
    [self.contentView addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.mas_top);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 30, 50));
    }];
    _bgView.backgroundColor = [UIColor whiteColor];
    
    _titleLabel = [[UILabel alloc] init];
    [_bgView addSubview:_titleLabel];
    _titleLabel = [[UILabel alloc] init];
    [_bgView addSubview:_titleLabel];
    CGFloat width = [@"使用条件：" getSizeOfString:16].width + 5;
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bgView.mas_top).offset(10);
        make.left.equalTo(_bgView.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _titleLabel.font = FONT(16);
    _titleLabel.text = _title;
    
    width = [_thrTitle getSizeOfString:16].width;
    _thrTitleLabel = [[UILabel alloc] init];
    [_bgView addSubview:_thrTitleLabel];
    [_thrTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_bgView.mas_right).offset(-10);
        make.top.equalTo(_titleLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _thrTitleLabel.text = _thrTitle;
    _thrTitleLabel.font = FONT(16);
    _thrTitleLabel.textColor = [UIColor lightGrayColor];
    
    width = [_secTitle getSizeOfString:16].width + 5;
    _secTitleLabel = [[UILabel alloc] init];
    [_bgView addSubview:_secTitleLabel];
    [_secTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel.mas_right).offset(10);
        make.top.equalTo(_titleLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _secTitleLabel.font = FONT(16);
    _secTitleLabel.text = _secTitle;
    _secTitleLabel.textColor = [UIColor lightGrayColor];
    
    _textField = [[UITextField alloc] init];
    [_bgView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_secTitleLabel.mas_right).offset(5);
        make.top.equalTo(_secTitleLabel.mas_top);
        make.right.equalTo(_thrTitleLabel.mas_left).offset(-5);
        make.bottom.equalTo(_bgView.mas_bottom).offset(- 10);
    }];
    _textField.textAlignment = NSTextAlignmentRight;
    _textField.font = FONT(16);
    _textField.delegate = self;
    
    _lineView = [[UIView alloc] init];
    [_bgView addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_textField.mas_left);
        make.bottom.equalTo(_bgView.mas_bottom).offset(-8);
        make.top.equalTo(_textField.mas_bottom).offset(1);
        make.right.equalTo(_textField.mas_right);
    }];
    _lineView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.conditionBlock) {
        self.conditionBlock(textField.text);
    }
}
#pragma mark - 隐藏键盘
- (void)hideKeyBoard{
    [_textField resignFirstResponder];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"hideAimKeyBoard" object:nil];
}
@end
